import 'dart:ui';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:toatool/src/data/model/upgrade.dart';
import 'package:toatool/toatool.dart';

enum DialogType {
  text,
  icon,
  upgrade,
}

class CustomDialogWidget {
  final DialogType type;
  final BuildContext ctx;
  final String? title;
  final String? content;
  final String? iconUrl;
  final String? iconText;
  final VoidCallback? onConfirm;
  final VoidCallback? onCancel;
  final String? onConfirmStr;
  final String? onCancelStr;
  final bool? isUpdateApp;
  final bool? isForcedUpdate; //是否强制更新（true关闭安卓物理按钮和侧滑返回,默认不关闭）
  final Upgrade? upgrade;
  final Widget? extContent;
  final num? insetPadding;
  bool barrierDismissible = true;

  CustomDialogWidget({
    required this.type,
    required this.ctx,
    this.title,
    this.content,
    this.iconUrl,
    this.iconText,
    this.onConfirm,
    this.onCancel,
    this.onConfirmStr,
    this.onCancelStr,
    this.isUpdateApp,
    this.isForcedUpdate,
    this.upgrade,
    this.extContent,
    this.insetPadding,
    required this.barrierDismissible,
  });

  Future<bool?> customShowDialog() {
    return showDialog<bool>(
        context: ctx,
        barrierDismissible: barrierDismissible,
        builder: (BuildContext buildContext) {
          return WillPopScope(
            onWillPop: () async {
              return isForcedUpdate == true ? false : true;
            },
            child: BackdropFilter(
                filter: ImageFilter.blur(sigmaX: 4, sigmaY: 4),
                child: Dialog(
                  insetPadding: EdgeInsets.symmetric(
                      horizontal: ScreenUtil().setWidth(insetPadding ?? 32.0)),
                  child: Container(
                    padding: EdgeInsets.all(ScreenUtil().setWidth(32.0)),
                    child: SingleChildScrollView(
                      child: Column(mainAxisSize: MainAxisSize.min, children: <
                          Widget>[
                        // 文本
                        if (type == DialogType.text)
                          Column(children: [
                            if (isUpdateApp != null) appUpdateIcon(),
                            if (title != null)
                              Text(
                                title!,
                                style: Theme.of(buildContext)
                                    .textTheme
                                    .headline6!
                                    .copyWith(
                                      color: blackTextColor,
                                      height: 1.33,
                                    ),
                              ),
                            SizedBox(
                              height: ScreenUtil().setHeight(32),
                            ),
                            if (content != null)
                              Text(
                                content!,
                                style: Theme.of(buildContext)
                                    .textTheme
                                    .bodyText2!
                                    .copyWith(
                                      height: 1.43,
                                      color: blackTextColor,
                                    ),
                              ),
                            SizedBox(
                              height: ScreenUtil().setHeight(26),
                            ),
                            if (extContent != null) extContent!,
                          ])
                        // 图文
                        else if (type == DialogType.icon)
                          Column(
                            children: [
                              if (iconUrl != null)
                                Image.asset(
                                  iconUrl!,
                                  width: ScreenUtil().setWidth(120.0),
                                  height: ScreenUtil().setHeight(120.0),
                                ),
                              SizedBox(
                                height: ScreenUtil().setHeight(16.0),
                              ),
                              if (iconText != null)
                                Text(
                                  iconText!,
                                  style: Theme.of(buildContext)
                                      .textTheme
                                      .subtitle1!
                                      .copyWith(fontWeight: FontWeight.w400),
                                ),
                              SizedBox(
                                height: ScreenUtil().setHeight(32.0),
                              ),
                            ],
                          )
                        // 应用升级
                        else if (type == DialogType.upgrade)
                          Column(children: [
                            Image.asset(
                              'assets/icons/logo.png',
                              width: ScreenUtil().setWidth(70.0),
                              height: ScreenUtil().setHeight(70.0),
                            ),
                            SizedBox(
                              height: ScreenUtil().setWidth(8),
                            ),
                            Text(
                              'v${upgrade!.appVerisonName}',
                              style:
                                  Theme.of(ctx).textTheme.bodyText2!.copyWith(
                                        color: blackTextColor,
                                        height: 1.43,
                                      ),
                            ),
                            SizedBox(
                              height: ScreenUtil().setHeight(24),
                            ),
                            Text(
                              'バージョンアップ',
                              style:
                                  Theme.of(ctx).textTheme.headline6!.copyWith(
                                        color: blackTextColor,
                                        height: 1.33,
                                      ),
                            ),
                            SizedBox(
                              height: ScreenUtil().setHeight(32),
                            ),
                            Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: upgrade!.contents
                                    .asMap()
                                    .entries
                                    .map(
                                      (e) => Text(
                                        '${e.key + 1}、${e.value}',
                                        style: Theme.of(ctx)
                                            .textTheme
                                            .bodyText2!
                                            .copyWith(
                                                color: blackTextColor,
                                                height: 1.43),
                                      ),
                                    )
                                    .toList()),
                            SizedBox(
                              height: ScreenUtil().setHeight(32),
                            )
                          ]),
                        if (onConfirm != null)
                          ElevatedButton(
                              onPressed: onConfirm,
                              child: Container(
                                  alignment: Alignment.center,
                                  //width: ScreenUtil().setWidth(247.0),
                                  height: ScreenUtil().setHeight(40.0),
                                  child: Text(
                                    onConfirmStr ?? '',
                                    style: Theme.of(buildContext)
                                        .textTheme
                                        .button!
                                        .copyWith(
                                          fontSize: ScreenUtil().setSp(14),
                                          color: Colors.white,
                                        ),
                                  ))),
                        if (onCancel != null)
                          SizedBox(
                            height: ScreenUtil().setHeight(14.0),
                          ),
                        if (onCancel != null)
                          ElevatedButton(
                              style: Theme.of(buildContext)
                                  .elevatedButtonTheme
                                  .style!
                                  .copyWith(
                                    backgroundColor: MaterialStateProperty.all(
                                      inputHitTextColor,
                                    ),
                                  ),
                              onPressed: onCancel,
                              child: Container(
                                  alignment: Alignment.center,
                                  //width: ScreenUtil().setWidth(247.0),
                                  height: ScreenUtil().setHeight(40.0),
                                  child: Text(
                                    onCancelStr ?? '',
                                    style: Theme.of(buildContext)
                                        .textTheme
                                        .button!
                                        .copyWith(
                                          color: blackTextColor,
                                          fontSize: ScreenUtil().setSp(14),
                                        ),
                                  ))),
                      ]),
                    ),
                  ),
                )),
          );
        });
  }

  Widget appUpdateIcon() {
    return Column(
      children: [
        Image.asset(
          iconUrl!,
          width: ScreenUtil().setWidth(70.0),
          height: ScreenUtil().setHeight(70.0),
        ),
        SizedBox(
          height: ScreenUtil().setHeight(18.0),
        ),
        if (iconText != null)
          Text(
            iconText!,
            style: TextStyle(
              color: blackTextColor,
              fontSize: 14,
            ),
          ),
        SizedBox(
          height: ScreenUtil().setHeight(24.0),
        ),
      ],
    );
  }
}
